package com.bmatch.yikaowx.web.controller;

import com.bmatch.yikaowx.web.dao.UserMapper;
import com.bmatch.yikaowx.web.dataobject.User;
import com.bmatch.yikaowx.web.model.Result;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;


@Controller
public class ExaminerControl {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping(path = "/examiner/add")
    @ResponseBody
    public Result addExaminer(@RequestBody User user) {
        Result result = new Result();
        if (StringUtils.isBlank(user.getName()) || StringUtils.isBlank(user.getMobile())) {
            result.setSuccess(false);
        } else if (userMapper.queryUser(user.getMobile()) == null && Pattern.matches("^([1][3,4,5,6,7,8,9])\\d{9}$", user.getMobile())) {
            userMapper.insert(user);
            result.setSuccess(true);
        }
        return result;
    }

    @RequestMapping(path = "/examiner/query")
    @ResponseBody
    public Result query(@RequestParam(value = "keyword", required = false) String keyWord) {
        List<User> examiners = new ArrayList();
        Result result = new Result();
        if (StringUtils.isBlank(keyWord)) {
            examiners = userMapper.selectAll();
            result.setSuccess(true);
            result.setUsers(examiners);
        } else {
            examiners = userMapper.selectByKeyWord(keyWord);
            result.setSuccess(true);
            result.setUsers(examiners);
        }
        return result;
    }

    @RequestMapping(path = "/examiner/update")
    @ResponseBody
    public Result update(@RequestBody User user) {
        Result result = new Result();
        if (StringUtils.isBlank(user.getName()) || StringUtils.isBlank(user.getMobile()) || user.getId() == null) {
            result.setSuccess(false);
        } else {
            User examiner = userMapper.selectByPrimaryKey(user.getId());
            if (examiner != null && Pattern.matches("^([1][3,4,5,6,7,8,9])\\d{9}$", user.getMobile())) {
                userMapper.updateByPrimaryKey(user);
                result.setSuccess(true);
            } else result.setSuccess(false);
        }
        return result;
    }

    @RequestMapping(path = "/examiner/delete")
    @ResponseBody
    public Result delete(@RequestBody User user) {
        Result result = new Result();
        if (StringUtils.isBlank(user.getName()) || StringUtils.isBlank(user.getMobile()) || user.getId() == null) {
            result.setSuccess(false);
        } else {
            User examiner = userMapper.selectByPrimaryKey(user.getId());
            if (examiner.getName().equals(user.getName()) && examiner.getMobile().equals(user.getMobile())) {
                userMapper.deleteByPrimaryKey(user.getId());
                result.setSuccess(true);
            } else result.setSuccess(false);
        }
        return result;
    }

    @RequestMapping(path = "/test")
    @ResponseBody
    public String test() {
        userMapper.deleteByPrimaryKey(7l);
        return "删除成功";
    }

}
